package com.zfei.gfb.controller;

import com.google.common.collect.Lists;
import com.zfei.gfb.api.StatsService;
import com.zfei.gfb.api.domain.StatsTag;
import com.zfei.gfb.api.domain.StatsValue;
import com.zfei.gfb.bean.Const;
import com.zfei.gfb.core.HttpJson;
import com.zfei.gfb.core.LogResult;
import com.zfei.gfb.dao.GfbLandPublicityMapper;
import com.zfei.gfb.model.GfbJob;
import com.zfei.gfb.model.GfbLandPublicity;
import com.zfei.gfb.query.GfbLandPublicityQuery;
import com.zfei.gfb.query.h5.H5LandPublicityQuery;
import com.zfei.gfb.request.GfbGetRequest;
import com.zfei.gfb.util.Beans;
import com.zfei.gfb.util.Now;
import com.zfei.gfb.vo.GfbJobVo;
import com.zfei.gfb.vo.GfbLandPublicityListVo;
import com.zfei.gfb.vo.GfbLandPublicityVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author
 * @version 1.0
 */
@Api(tags = "H5土地，池塘，山林【中标】接口")
@RestController
@RequestMapping("/h5/land/publicity")
public class H5LandPublicityController {

    @Resource
    private GfbLandPublicityMapper gfbLandPublicityMapper;

    @Resource
    private StatsService statsService;

    @ApiOperation(value = "列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "", response = GfbLandPublicityListVo.class),
    })
    @LogResult
    @HttpJson("/list")
    public Object list(@RequestBody H5LandPublicityQuery query) {
        query.check().build(Now::millis);

        GfbLandPublicityListVo result = new GfbLandPublicityListVo();

//        int total = gfbLandPublicityMapper.total(query);
//        result.setTotal(total);
//        if (total == 0) {
//            result.setData(Lists.newArrayList());
//            return result;
//        }

        List<GfbLandPublicity> list = gfbLandPublicityMapper.listH5(query);
        List<GfbLandPublicityVo> gfbLandPublicityVos = Beans.copys(list, GfbLandPublicityVo.class);

        result.setData(gfbLandPublicityVos);
        return result;

    }

    @ApiOperation(value = "中标详情")
    @ApiResponses({
            @ApiResponse(code = 200, message = "",response = GfbLandPublicityVo.class),
    })
    @LogResult
    @HttpJson("/get")
    public Object get(@RequestBody GfbGetRequest request) {
        request.checkGetNull();
        Integer id = request.getId();

        GfbLandPublicity record = gfbLandPublicityMapper.selectByPrimaryKey(id);

        GfbLandPublicityVo vo = Beans.copy(record, GfbLandPublicityVo.class);

        StatsValue postStats;
        switch (record.getType()){
            case Const.LAND:
                postStats = new StatsValue(record.getId().toString(), StatsTag.LAND_PUBLICITY_BROWSE_COUNT, 1);
                statsService.inc(postStats);
                vo.setBrowseNum(statsService.get(record.getId().toString(), StatsTag.LAND_PUBLICITY_BROWSE_COUNT).getNum());
                break;
            case Const.POOL:
                postStats = new StatsValue(record.getId().toString(), StatsTag.POND_PUBLICITY_BROWSE_COUNT, 1);
                statsService.inc(postStats);
                vo.setBrowseNum(statsService.get(record.getId().toString(), StatsTag.POND_PUBLICITY_BROWSE_COUNT).getNum());
                break;
            case Const.FOREST:
                postStats = new StatsValue(record.getId().toString(), StatsTag.MOUNTAIN_PUBLICITY_BROWSE_COUNT, 1);
                statsService.inc(postStats);
                vo.setBrowseNum(statsService.get(record.getId().toString(), StatsTag.MOUNTAIN_PUBLICITY_BROWSE_COUNT).getNum());
                break;
            default:
                break;
        }
        return vo;
    }

}
